/*
    判断一个值是否为数字
*/
export function isNumber(val) {
    const regPos = /^\d+(\.\d+)?$/,
        regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/;
    return regPos.test(val) || regNeg.test(val);
}
/*
    将整数转成小数，并保留对应的位数
*/
export function decFloat(f, digit = 2) {
    if (isNumber(f)) {
        const m = Math.pow(10, digit);
        return parseInt(decMul(f, m, -1), 10) / m;
    } else {
        return 0;
    }
}
/*
    加法运输
*/
export function decAdd(num1, num2, digit=2) {
    if (isNumber(num1) && isNumber(num2)) {
        const num1Digits = (num1.toString().split(".")[1] || "").length,
            num2Digits = (num2.toString().split(".")[1] || "").length,
            baseNum = Math.pow(10, Math.max(num1Digits, num2Digits)),
            calcValue = (decMul(num1, baseNum) + decMul(num2, baseNum)) / baseNum;
        if (digit === -1) {
            return calcValue;
        } else {
            return decFloat(calcValue, digit);
        }
    } else {
        return 0;
    }
}
/*
    减法运算
*/
export function decSub(num1=0, num2=2, digit=2) {
    if (isNumber(num1) && isNumber(num2)) {
        const num1Digits = (num1.toString().split(".")[1] || "").length,
            num2Digits = (num2.toString().split(".")[1] || "").length,
            baseNum = Math.pow(10, Math.max(num1Digits, num2Digits)),
            calcValue = (decMul(num1, baseNum) - decMul(num2, baseNum)) / baseNum;
        if (digit === -1) {
            return calcValue;
        } else {
            return decFloat(calcValue, digit);
        }
    } else {
        return 0;
    }
}
/**
    乘法运算
*/
export function decMul(num1=0, num2=2, digit=2) {
    if (isNumber(num1) && isNumber(num2)) {
        const num1String = num1.toString(),
            num2String = num2.toString(),
            num1Digits = (num1String.split(".")[1] || "").length,
            num2Digits = (num2String.split(".")[1] || "").length,
            baseNum = Math.pow(10, num1Digits + num2Digits),
            calcValue = (Number(num1String.replace(".", "")) * Number(num2String.replace(".", ""))) / baseNum;
        if (digit === -1) {
            return calcValue;
        } else {
            return decFloat(calcValue, digit);
        }
    } else {
        return 0;
    }
}
/**
   除法运算
*/
export function decDiv(num1=0, num2=2, digit=2) {
    if (isNumber(num1) && isNumber(num2)) {
        const num1String = num1.toString(),
            num2String = num2.toString(),
            num1Digits = (num1String.split(".")[1] || "").length,
            num2Digits = (num2String.split(".")[1] || "").length,
            baseNum = Math.pow(10, num1Digits + num2Digits),
            n1 = decMul(num1, baseNum),
            n2 = decMul(num2, baseNum),
            calcValue = Number(n1) / Number(n2);
        if (digit === -1) {
            return calcValue;
        } else {
            return decFloat(calcValue, digit);
        }
    } else {
        return 0;
    }
}
/*
    银行家舍入
    舍去位的数值小于5时，直接舍去。
    舍去位的数值大于等于6时，进位后舍去。
    舍去位的数值等于5时，如果5后面没有其他数字且5前面的数字是偶数，则直接舍去；如果5前面的数字是奇数，则进位后舍去。
*/
export function ROUND_HALF_EVEN(num) {
    const numArr = (num + "").split(".");
    if (numArr.length == 2) {
        const one = numArr[1].slice(0, 1);
        if (one == 5) {
            const sliceEnd = numArr[1].slice(1);
            if (parseInt(sliceEnd || 0)) {
                return parseInt(numArr[0]) + 1;
            } else {
                const end = numArr[0].slice(-1);
                if (end % 2) {
                    return parseInt(numArr[0]) + 1;
                } else {
                    return parseInt(numArr[0]);
                }
            }
        } else if (parseFloat(one) > 5) {
            return parseInt(numArr[0]) + 1;
        } else {
            return parseInt(numArr[0]);
        }
    } else {
        return parseInt(numArr[0]);
    }
}
